package cn.ht.bootdemo.common.aop;

import cn.ht.bootdemo.common.exception.ArgsNullException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;


/**
 * @author ht
 * @Description:
 * @date 2020-5-13
 */
@Aspect
@Component
public class CheckUserAop {

    @Pointcut("@annotation(cn.ht.bootdemo.common.anno.CheckUser)")
    public void annotationPointcut() {

    }
    @Before("annotationPointcut()")
    public void beforePointcut(JoinPoint joinPoint) {
        // 此处进入到方法前  可以实现一些业务逻辑
    }

    @Around("annotationPointcut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        // 获取参数名称
        String[] params = methodSignature.getParameterNames();
        // 获取参数值
        Object[] args = joinPoint.getArgs();
        if (null == params || params.length == 0){
            throw new ArgsNullException("type不能为空");
        }
        return joinPoint.proceed();
    }

    @AfterReturning(returning = "res",pointcut = "annotationPointcut()")
    public void doAfterReturning(String res) {
        System.out.println(res);
    }
}
